package my.spring.aop.pointcut.method;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 方法匹配的注解版实现
 * @author fengbo
 * @date 2018/7/1
 */
public class AnnotationMethodMatcher implements MethodMatcher {

    private final Class<? extends Annotation> annotationType;

    public AnnotationMethodMatcher(Class<? extends Annotation> annotationType) {
        this.annotationType = annotationType;
    }

    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        if (method.isAnnotationPresent(this.annotationType)) {
            return true;
        }
        Method specificMethod;
        try {
            specificMethod = targetClass.getMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException e) {
            specificMethod = method;
        }
        return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
    }

}
